/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.controller;

import com.bjboot.caocao.common.config.Constant;
import com.bjboot.caocao.common.utils.Result;
import com.bjboot.caocao.system.domain.DeptDO;
import com.bjboot.caocao.system.domain.Tree;
import com.bjboot.caocao.system.service.DeptService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Description 部门管理对应的控制器
 *@Author 操文健
 *@Date 2018-09-12 上午11:36
 *@Version 1.0
 **/
@Controller
@RequestMapping("/system/sysDept")
public class DeptController extends UserBaseController {
    // templates位置
    private String prefix = "system/dept";
    @Autowired
    private DeptService sysDeptService;

    /**
     * 返回访问部门管理对应的主界面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("system:sysDept:sysDept")
    String dept() {
        return prefix + "/dept";
    }

    /**
     * @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response =
     * “接口返回参数类型”, notes = “接口发布说明”；
     * @return
     */
    @ApiOperation(value="获取部门列表", notes="")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:sysDept:sysDept")
    public List<DeptDO> list() {
        Map<String, Object> query = new HashMap<>(16);
        List<DeptDO> sysDeptList = sysDeptService.list(query);
        return sysDeptList;
    }

    @ApiOperation(value="弹出新增部门界面", notes="")
    @GetMapping("/add/{pId}")
    @RequiresPermissions("system:sysDept:add")
    String add(@PathVariable("pId") Long pId, Model model) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", Constant.DEPT_ROOT_NAME);
        } else {
            model.addAttribute("pName", sysDeptService.get(pId).getName());
        }
        return  prefix + "/add";
    }

    @ApiOperation(value="弹出修改部门界面", notes="")
    @GetMapping("/edit/{deptId}")
    @RequiresPermissions("system:sysDept:edit")
    String edit(@PathVariable("deptId") Long deptId, Model model) {
        DeptDO sysDept = sysDeptService.get(deptId);
        model.addAttribute("sysDept", sysDept);
        if(Constant.DEPT_ROOT_ID.equals(sysDept.getParentId())) {
            model.addAttribute("parentDeptName", Constant.DEPT_ROOT_NAME);
        }else {
            DeptDO parDept = sysDeptService.get(sysDept.getParentId());
            model.addAttribute("parentDeptName", parDept.getName());
        }
        return  prefix + "/edit";
    }

    @ApiOperation(value="保存部门信息", notes="")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:sysDept:add")
    public Result save(DeptDO sysDept) {
        if (sysDeptService.save(sysDept) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value="更新部门信息", notes="")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:sysDept:edit")
    public Result update(DeptDO sysDept) {
        if (sysDeptService.update(sysDept) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value="删除部门", notes="")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:remove")
    public Result remove(Long deptId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", deptId);
        if(sysDeptService.count(map)>0) {
            return Result.error(1, "包含下级部门,不允许修改");
        }
        if(sysDeptService.checkDeptHasUser(deptId)) {
            if (sysDeptService.remove(deptId) > 0) {
                return Result.ok();
            }
        }else {
            return Result.error(1, "部门包含用户,不允许修改");
        }
        return Result.error();
    }

    @ApiOperation(value="批量删除部门", notes="")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:batchRemove")
    public Result remove(@RequestParam("ids[]") Long[] deptIds) {
        sysDeptService.batchRemove(deptIds);
        return Result.ok();
    }

    @ApiOperation(value="获取部门树", notes="")
    @GetMapping("/tree")
    @ResponseBody
    public Tree<DeptDO> tree() {
        Tree<DeptDO> tree = new Tree<DeptDO>();
        tree = sysDeptService.getTree();
        return tree;
    }

    @ApiOperation(value="弹出部门树templete", notes="")
    @GetMapping("/treeView")
    String treeView() {
        return  prefix + "/deptTree";
    }
}
